package com.moogene.saad.gameplay.stages;

import java.util.ArrayList;
import java.util.Collection;

import android.graphics.Canvas;

import com.moogene.android.InfoMediator;
import com.moogene.android.engine.mmi.Layer;
import com.moogene.android.engine.mmi.layout.GirdLayout;
import com.moogene.android.engine.mmi.layout.Layout;
import com.moogene.saad.gameplay.defender.TowerTemplate;

public class TowerSelector extends Layer {
	private final static int SELECTOR_SPEED = 8;
	private final int WIDTH_TOWER_SELECTOR = (InfoMediator.getScreenWidth() >> 2);
	private final static int HEIGHT_SELECTOR_ITEM = 90;
	
	boolean isScrollIn;
	boolean isScrollOut;
	BattleField linkedBattleField;
	Layout layout;

	public TowerSelector() {
		setLocation(InfoMediator.getScreenWidth() - WIDTH_TOWER_SELECTOR, 0);
		setSize(WIDTH_TOWER_SELECTOR, 1000);
		setPriority(Layer.HUD);
		layout = new GirdLayout(-1, 2,
				HEIGHT_SELECTOR_ITEM);
		setLayout(layout);
	}


	protected boolean canDragVertical() {
		return true;
	}

	protected boolean willfollowCameraWhenDrag() {
		return false;
	}
	
	void link(BattleField linkBattleField){
		linkedBattleField = linkBattleField;
	}
	
	void fill(Collection<TowerTemplate> collection){
		clear();
		for (TowerTemplate t : collection) {
			addLayer(t);
		}
	}

	@Override
	public void update() {
		super.update();
		if (linkedBattleField.isDragging() && !isHold()) {
			isScrollIn = true;
			isScrollOut = false;
		} else {
			isScrollIn = false;
			isScrollOut = true;
			linkedBattleField.setFollowCameraWhenDrag(false);
		}
		if (isScrollIn) {
			if (x >= InfoMediator.getScreenWidth()) {
				isScrollIn = false;
				linkedBattleField.setFollowCameraWhenDrag(true);
			} else {
				x += SELECTOR_SPEED;
			}

		} else if (isScrollOut) {
			if (x <= InfoMediator.getScreenWidth() - WIDTH_TOWER_SELECTOR) {
				isScrollOut = false;
			} else {
				x -= SELECTOR_SPEED;
			}
		}
	}

	@Override
	public void draw(Canvas canvas) {
		fillColor(canvas, 0x3FB7DCFF);
		super.draw(canvas);
	}
}
